package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.pojo.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * controller的方法中主要完成哪些事情?
     *  1. 接收请求的参数并封装参数
     *  2. 调用service方法完成业务逻辑的处理
     *  3. 构建返回值并返回
     *
     * 建议:
     *  我们在调用每个方法的时候都建议把方法的参数输出到日志文件中，方便我们后期定位问题
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result<String> save(@RequestBody EmployeeDTO dto){
        log.info("新增员工传输的参数: {}", dto);
        return employeeService.save(dto);
    }

    @GetMapping("/page")
    public Result<PageResult> queryPage(EmployeePageQueryDTO queryDTO){
        log.info("分页查询员工传输的参数: {}", queryDTO);
        return employeeService.queryPage(queryDTO);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("禁用启用员工账号")
    public Result<String> startOrStop(@PathVariable("status") Integer status,@RequestParam("id") Long empId){
        log.info("禁用启用员工账号: {}--{}", status, empId);
        return employeeService.startOrStop(status, empId);
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询员工")
    public Result<EmployeeVO> selectById(@PathVariable Long id) throws CloneNotSupportedException {
        log.info("根据id查询员工: {}", id);

        return employeeService.selectById(id);
    }

    @PutMapping
    @ApiOperation("更新员工信息")
    public Result<String> updateById(@RequestBody EmployeeDTO dto) throws CloneNotSupportedException {
        log.info("更新员工信息: {}", dto);

        return employeeService.updateById(dto);
    }
}
